﻿package
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	
	public class BtnRoll_Jump
	{
		//VARIABLES INTERNAS SETEABLES OBLIGATORIAS.
			
			public var clip:MovieClip;
		
		//VARIABLES INTERNAS SETEABLES OPCIONALES.
			
			public var useHand:Boolean = true;
			public var g:Number = 1;
			public var overAcc:Number = 15;
			public var hitFactor:Number = 0.5;
			public var jumpDelay:Number = 2;
			
		//VARIABLES INTERNAS.
		private var initY:Number;
		private var vY:Number = 0;
		private var jumpTimer:Timer;
		
		//CONSTRUCTOR.
		public function BtnRoll_Jump()
		{
		
		}
		
		//INIT.
		public function init()
		{
			//SETEA LA MANITO.
			clip.hit.buttonMode = useHand;
			clip.hit.useHandCursor = useHand;
			
			//REGISTRA LA POSICION INICIAL.
			initY = clip.subClip.y;
			
			//INICIALIZA EL TIMER.
			jumpTimer = new Timer(jumpDelay*1000);
			jumpTimer.addEventListener("timer", accel);
			
			//ACTIVA EL ROLL OVER Y ROLL OUT DEL CLIP.
			clip.hit.addEventListener(MouseEvent.MOUSE_OVER, rollOverHandler);
			clip.hit.addEventListener(MouseEvent.MOUSE_OUT, rollOutHandler);
			
			//INICIA EL ENTERFRAME.
			clip.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		//MOUSE HANDLERS.
		private function rollOverHandler(event:MouseEvent):void
		{
			//INICIA EL TIMER.
			accel(null);
			jumpTimer.start();
		}
		private function rollOutHandler(event:MouseEvent):void
		{
			//CORTA EL TIMER.
			jumpTimer.stop();
		}
		
		//ACELERA HACIA ARRIBA
		private function accel(evt:TimerEvent):void
		{
			//ACELERA.
			if(clip.subClip.y == initY)
				vY -= overAcc;
		}
			
		//ENTERFRAME.
		private function enterFrameHandler(evt:Event):void
		{
			//GRAVEDAD.
			vY += g;
			
			//MOVIMIENTO.
			clip.subClip.y += vY;
			
			//CONTENCION.
			if(clip.subClip.y > initY)
			{
				clip.subClip.y = initY;
				vY *= -hitFactor;
			}
		}
	}
}